package com.vogue.supplymall.account.controller;

//import com.vogue.searchengin.es.ESClient;
//import org.elasticsearch.action.ActionFuture;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.delete.DeleteResponse;
//import org.elasticsearch.action.get.GetRequest;
//import org.elasticsearch.action.index.IndexRequestBuilder;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.search.*;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.transport.TransportClient;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.index.reindex.BulkByScrollResponse;
//import org.elasticsearch.index.reindex.DeleteByQueryAction;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//import javax.servlet.http.HttpServletRequest;
//import java.util.*;
//import java.util.concurrent.TimeUnit;

@RestController
public class TestController extends BaseController {

//	private String idx="doc";
//    private String type="class";
//    private TransportClient client=ESClient.getClient("192.168.7.231",9300);
//
//    @RequestMapping( value = "/createindex",method = RequestMethod.GET)
//    public String createindex(HttpServletRequest request){
//        IndexResponse idxres=client.prepareIndex(idx,type).execute().actionGet();
//        return idxres.toString();
//    }
//
//    @RequestMapping( value = "/createdoc",method = RequestMethod.GET)
//    public String createdoc(HttpServletRequest request){
//        IndexRequestBuilder idxreq=client.prepareIndex(idx,type);
//        Map data=new HashMap<>();
//        data.put("name","made in china");
//        data.put("desc","1");
//        idxreq.setId("1");
//        idxreq.setSource(data).execute();
//        //data=new HashMap<>();
//        data.put("name","made in galxy");
//        data.put("desc","2");
//        idxreq.setId("2");
//        idxreq.setSource(data).execute();
//        //data=new HashMap<>();
//        data.put("name","noused ");
//        data.put("desc","3");
//        idxreq.setId("3");
//        IndexResponse idxres=idxreq.setSource(data).execute().actionGet();
//        //
//        return idxres.toString();
//    }
//
//
//    @RequestMapping( value = "/getall",method = RequestMethod.GET)
//    public String getall(HttpServletRequest request){
////        GetRequest getr=new GetRequest();
//        SearchRequest sr=new SearchRequest();
//        sr.indices(idx).types(type);
////        getr.index(idx).type(type);
//        return client.search(sr).actionGet().toString();
//    }
//
//    @RequestMapping( value = "/delall",method = RequestMethod.GET)
//    public String delall(HttpServletRequest request){
//        SearchRequest sr=new SearchRequest();
//        sr.indices(idx).types(type);
//        SearchResponse sres=client.search(sr).actionGet();
//        Iterator<SearchHit> its=sres.getHits().iterator();
//        String ret=" ";
//        while (its.hasNext()){
//            SearchHit hit=its.next();
//            String id=hit.getId();
//            ret+=id;
//            DeleteRequest dr=new DeleteRequest();
//            dr.index(idx).type(type).id(id);
//            client.delete(dr).actionGet();
//        }
//        return ret;
//    }
//
//    @RequestMapping( value = "/updatedoc",method = RequestMethod.GET)
//    public String updatedoc(HttpServletRequest request){
//        String desc=request.getParameter("desc");
//        Map data=new HashMap<>();
//        data.put("name","made in china");
//        data.put("desc",desc);
//        UpdateRequest ur=new UpdateRequest();
//        ur.index(idx).type(type).id("1").doc(data);
//        UpdateResponse ures=client.update(ur).actionGet();
//        return ures.toString();
//
////        IndexRequestBuilder idxreq=client.prepareIndex(idx,type);
////        Map data=new HashMap<>();
////        data.put("name","made in china");
////        data.put("desc",desc);
////        idxreq.setId("1");
////        IndexResponse idxres=idxreq.setSource(data).execute().actionGet();
////        //
////        return idxres.toString();
//    }
//
//    @RequestMapping( value = "/search",method = RequestMethod.GET)
//    public String search(HttpServletRequest request){
////        QueryBuilder qb = QueryBuilders.queryStringQuery( "一 行");
//        QueryBuilder qb = QueryBuilders.rangeQuery("desc").gt("1").lt("3");
//        SearchResponse scrollResp = client.prepareSearch().setTypes(type)
//                .setQuery(qb)//.addSort("desc", SortOrder.ASC)
////                .setScroll(new TimeValue(60000))
//                .setFrom(0)
//                .setSize(2)
//                .get();
//        //scrollResp.getHits().getHits()[0].getSourceAsString().toString();
//        return scrollResp.toString();
//
////        SearchResponse response = client.prepareSearch(idx)
////                .setTypes(type)
////                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
////                .setQuery(QueryBuilders.matchPhraseQuery("desc","一"))
//////                .setPostFilter(QueryBuilders.rangeQuery("desc").from(0).to(5))     // Filter
////                .setFrom(0).setSize(2)//.setExplain(true)
////                .get();
////        return response.toString();
//    }
//
//    @RequestMapping( value = "/bulkdelete",method = RequestMethod.GET)
//    public String bulkdelete(HttpServletRequest request){
//        BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
//            .filter(QueryBuilders.matchQuery("desc", "一"))
//            .source(idx)
//            .get();
//        return response.toString();
//    }
//
//    @RequestMapping( value = "/singledelete",method = RequestMethod.GET)
//    public String singledelete(HttpServletRequest request){
//        DeleteRequest dr=new DeleteRequest();
//        dr.index(idx).type(type).id("-1");
//        ActionFuture<DeleteResponse> ftres=client.delete(dr);
//        DeleteResponse res=ftres.actionGet(1, TimeUnit.SECONDS);
////        DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")
////                .get();
//        return res.toString();
//    }
//
//    @RequestMapping( value = "/query",method = RequestMethod.GET)
//    public String query(HttpServletRequest request){
//        QueryBuilder qb = QueryBuilders.termQuery( "desc","一");
//        SearchRequestBuilder srb=client.prepareSearch(idx).setTypes(type).setQuery(qb);
//        SearchResponse sr=srb.get();
//        return sr.toString();
//    }
}
